<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let m1 = new Map([
            ["name", "twj"],
            ["age", 100],
            [{ a: 1 }, "北京"]
        ])
        console.log(m1) //{'name' => 'twj', 'age' => 100, {Object} => '北京'}

        let o = { a: 1 }
        let m2 = new Map()
        m2.set("name", "twj")
        m2.set("age", 100)
        m2.set(o, "北京")
        console.log(m2) //{'name' => 'twj', 'age' => 100, {Object} => '北京'}

        //使用for..of
        for (let i of m2) {
            console.log(i)
        }
        //使用扩展运算符+解构赋值
        console.log([...m2])

        //map实例的属性和方法
        console.log(m2.get(o)) //北京
        console.log(m2.has("age")) //true
        m2.delete("age")
        console.log(m2.has("age")) //false
        console.log(m2.size) //2
        //m2.clear() //清空map
        //console.log(m2)

        //map的遍历
        for (let i of m2.keys()) {
            console.log(i)
        }
        for (let i of m2.values()) {
            console.log(i)
        }
        for (let [index, item] of m2.entries()) {
            console.log(index, item)
        }
        //与entries()作用一样
        for (let [index, item] of m2) {
            console.log(index, item)
        }
        m1.forEach((item, index) => {
            console.log(item, index)
        })
    </script>
</body>

</html>